package org.cttic.visualization.visualization.z_service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.cttic.visualization.visualization.common.RealTimeTrafficPush;
import org.cttic.visualization.visualization.common.commonConf;
import org.cttic.visualization.visualization.common.realTimeData;
import org.cttic.visualization.visualization.util.ConvertColor;
import org.cttic.visualization.visualization.util.DataUtil;
import org.cttic.visualization.visualization.util.sevenUtil.RoadEventsUtil;
import org.cttic.visualization.visualization.util.z_DataUtil;
import org.cttic.visualization.visualization.websocket.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaHandler;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author shkstart
 * @create 2020-05-06 17:26
 */
@Component
@Slf4j
public class z_realTimeImpl {
    @Autowired
    z_DataUtil z_dataUtil;
    @Autowired
    private RoadEventsUtil roadEventsUtil;

    //指定监听的topic，当前消费者组id
    @KafkaHandler
    @KafkaListener(topics = commonConf.z_kafkaTopic)
    public void registryReceiver(List<ConsumerRecord<String, String>> records) throws ParseException {


        log.debug("接收到{}条", records.size());
        for (ConsumerRecord<String, String> record : records) {
            realTimeData data = z_dataUtil.sendDataUtil(record.value());
            if (data.getTransFee() != null && Double.parseDouble(data.getTransFee()) > 0.0 && Double.parseDouble(data.getTransFee()) < 500.0) {
                JSONObject jsonObject = JSONUtil.parseObj(data);
                String str = jsonObject.getStr("gantryId");
                String substring = str.substring(5, 7);
                if ("51".equals(substring)) {

                    System.out.println("发送消息："+data);
                    System.out.println("接受消息："+record.value());
                }

                Data.sendMessage(JSONUtil.parseObj(data));
            }else if(!data.getProvinceId().equals("36")){
                double random = Math.random() * 10 + 32 + Math.random() * 30;
                data.setTransFee(""+random);

                Data.sendMessage(JSONUtil.parseObj(data));
            }
        }

//        if (records.size() >= 10) {
//            String value = records.get(0).value();
//            //realTimePushTraffic(value);
//
//
////            }
//        } else {
//            String value = records.get(0).value();
//            //realTimePushTraffic(value);
//
//            for (int i = 0; i < records.size(); i++) {
//                double v = Math.random() * 10;
//                if (v >= 0) {
//
//                    realTimeData data = z_dataUtil.sendDataUtil(records.get(0).value());
//
//                    if (data.getTransFee() != null && Double.parseDouble(data.getTransFee()) > 0.0 && Double.parseDouble(data.getTransFee()) < 500.0) {
//
//                        Data.sendMessage(JSONUtil.parseObj(data));
//                    }
//
//                }
//
//            }
//        }
        //todo 发送收费站路况信息
        roadEventsUtil.roadEventQuery(records.get(0).value());

    }


    private void realTimePushTraffic(String value) {

        JSONObject json = new JSONObject(value);

        /*RealTimeTrafficPush trafficPush = new RealTimeTrafficPush();
        trafficPush.setVehicleId(json.getStr("vehiclePlate"));
        trafficPush.setVehicleColor(json.getStr("vehicleColor"));
        trafficPush.setTransTime(json.getStr("transTime"));
        trafficPush.setVec1(json.getInt("speed"));
        trafficPush.setLat(json.getStr("latitude"));
        trafficPush.setLng(json.getStr("longitude"));
        Data.sendMessage(JSONUtil.parseObj(trafficPush));
        */
        JSONObject data = new JSONObject();

        data.put("cmd", "189")
            .put("vehicleId", json.getStr("vehiclePlate"))
            .put("vehicleColor", ConvertColor.toColor(json.getInt("vehicleColor")))
            .put("transTime", json.getStr("transTime"))
            .put("vec1", json.getInt("speed"))
            //.put("direction","")
            .put("gantryId", json.getStr("gantryId"))
            .put("lat", json.getStr("latitude"))
            .put("lng", json.getStr("longitude"));

        Data.sendMessage(data);

    }
}
